//
//  PictureViewController.m
//  iOSPractice
//
//  Created by hoolai on 2018/11/19.
//  Copyright © 2018年 hoolai. All rights reserved.
//

#import "MenuViewController.h"

@interface MenuViewController ()
@property (weak, nonatomic) IBOutlet UIButton *listButton;
@property (weak, nonatomic) IBOutlet UIButton *startButton;
@property (strong, nonatomic) RoleViewController *vcTable;
@property (strong, nonatomic) LevelViewController *vcLevel;
@property (weak, nonatomic) IBOutlet UIButton *headButton;
@property (weak, nonatomic) IBOutlet UIButton *clearButton;

@end

@implementation MenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.labelWelcome.layer.cornerRadius = 5;
    self.labelWelcome.layer.masksToBounds = YES;
    
    UIStoryboard *sb=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.vcTable = [sb instantiateViewControllerWithIdentifier:@"vcTable"];
    self.vcLevel = [sb instantiateViewControllerWithIdentifier:@"vcLevel"];
    //读取user.plist文件  获取上一次的用户配置
    NSString *path = [[AppSettings dataFilePath]stringByAppendingString:@"/user.plist"];
    if([[NSFileManager defaultManager]fileExistsAtPath:path]){
        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
        [AppSettings shareSettings].level = [[dict valueForKey:@"level"] integerValue];
        [AppSettings shareSettings].myBeast = [MagicBeastsModel beastsWithDict:[dict valueForKey:@"beast"]];
    }
    
    //测试用。
    //[AppSettings shareSettings].level = 3;

}
//将动画效果改到viewDidAppear中执行，动画效果正常生效。
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    if([AppSettings shareSettings].myBeast == nil){
        [self.listButton setHidden:YES];//这个按钮的显示在动画中执行
        [self.headButton setHidden:YES];
        [self.startButton setHidden:YES];
        [self.clearButton setHidden:YES];
        [self welcomeAnimation];
    }else{
        UIImage *image = [UIImage imageNamed:[AppSettings shareSettings].myBeast.icon];
        [self.headButton setImage:image forState:UIControlStateNormal];
        [self.listButton setHidden:YES];
        [self.headButton setHidden:NO];
        [self.startButton setHidden:NO];
        [self.clearButton setHidden:NO];
    }
}

//执行动画效果 : welcome label的出现和消失
- (void)welcomeAnimation{
    /*
     参数详解   * 写给自己看的 *
     animateWithDuration : 动画执行时间
     animations : 动画执行代码
     completion : 动画执行完成后执行的代码
     */
    [UIView animateWithDuration:1.0 animations:^{
        self.labelWelcome.alpha = 0.8;
    } completion:^(BOOL finished) {
        NSLog(@"%d",finished);
        if(finished){
            [UIView animateWithDuration:1.0 delay:2.0 options:UIViewAnimationOptionCurveLinear animations:^{
                self.labelWelcome.alpha = 0.0;
            } completion:^(BOOL finished) {
                if(finished){
                    [self.labelWelcome removeFromSuperview];
                    self.listButton.hidden = NO;
                    //NSLog(@"welcome : %@",self.labelWelcome);
                }
            }];
        }
    }];
}

- (IBAction)listButtonTouched:(id)sender {
    [self jumpToRoleView];
}

- (void)jumpToRoleView{
    
    [self presentViewController:self.vcTable animated:NO completion:nil];
}


- (IBAction)startAdventure:(id)sender {
    //NSLog(@"角色:%ld 开始冒险",[AppSettings shareSettings].myBeast.ID);
    
    [self presentViewController:self.vcLevel animated:NO completion:nil];
}

- (IBAction)changeRole:(id)sender {
    [self jumpToRoleView];
}

- (IBAction)clearInfo:(id)sender {
    [AppSettings shareSettings].myBeast = nil;
    [AppSettings shareSettings].level = 1;
    NSString *path = [[AppSettings dataFilePath]stringByAppendingString:@"/user.plist"];
    if([[NSFileManager defaultManager] removeItemAtPath:path error:nil]){
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
    [self viewDidAppear:YES];
}

@end
